package com.sailmi.common.tool.crypto;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

import java.security.Security;
import java.util.Base64;

public class SM2OpenSSLDecryptTest {

	static {
		Security.addProvider(new BouncyCastleProvider());
	}
	public static void main(String[] args) {
		String secStr="306e022066363e81b47b17896954414f88c70c2aa23941a85383f69dc1b92161a9513009022100a8852d6b38d0fa754e267e3f1a6a47bf50d38fa9b72add1a1ef2b14407bd8b6b0420ee96de402639b25e5487b04a7220e58c81d3d059c5153c55d64150ec48036db804052c5b9dec04";
		String secStrWeb = "7eaa66b9337b74e01c13d9de4c9494f2bb94c4c18b4d3367959d3657353b5a6452413c7d304db17e0be14135b94d43e71275d75f3ace8bf89c63aabe6919271d5d34f08e961276e8b7a1d9e635bf2935950fa27f24c6a60cb9a3734371eb612976efe2a977";
		String privateStr = "726ac2141eaf6dcef4d279597fd21720d5f79ccefe9ebd7cd6cd5fdff9ab0c2d";
		String publicStr = "048eada0aa20c61f75e7b8b85c52f98e9bddfb9a614b4269f6e116dbd06b18aa8777c5797f13becb87750e799c2ccac7047c5cefdd223d4d7ca8b63a99d05093c4";
		try {
			String decStr = SM2Util.decryptPasswordEncryptByOpenSSL(secStr, privateStr);
			System.out.println("解码后结果为："+decStr);
			String decStrWeb1=SM2Util.decryptBySM2(Base64.getEncoder().encodeToString(Hex.decode(secStrWeb)),
				Base64.getEncoder().encodeToString(Hex.decode(privateStr)));
			System.out.println("解码后结果为："+decStrWeb1);
			String decStrWeb2=SM2Util.decryptPassword(secStrWeb,
													publicStr,
													privateStr);
			System.out.println("解码后结果为："+decStrWeb2);
		} catch (InvalidCipherTextException e) {
			throw new RuntimeException(e);
		}
	}

}
